package cn.enilu.flash.mobile.controller;

import cn.enilu.flash.bean.constant.factory.PageFactory;
import cn.enilu.flash.bean.entity.bus.ActionAttend;
import cn.enilu.flash.bean.entity.cms.Article;
import cn.enilu.flash.bean.entity.cms.Banner;
import cn.enilu.flash.bean.enumeration.Permission;
import cn.enilu.flash.bean.vo.front.Rets;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.mobile.domain.ArticleSearch;
import cn.enilu.flash.service.bus.ActionAttendService;
import cn.enilu.flash.service.cms.ArticleService;
import cn.enilu.flash.service.cms.BannerService;
import cn.enilu.flash.utils.Lists;
import cn.enilu.flash.utils.StringUtil;
import cn.enilu.flash.utils.factory.Page;
import cn.enilu.flash.web.controller.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.Param;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author ：enilu
 * @date ：Created in 11/4/2019 9:06 PM
 */
@RestController
@RequestMapping("/cms")
public class CmsController extends BaseController {

    @Autowired
    private BannerService bannerService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ActionAttendService actionAttendService;

    @GetMapping(value = "/banners")
    public Object list(@RequestParam(required = false) String type) {
        SearchFilter filter = null;
        if(StringUtil.isNotEmpty(type)){
            filter =  SearchFilter.build("type", SearchFilter.Operator.EQ, type);
        }
        List<Banner> list = bannerService.queryAll(filter);
        return Rets.success(list);
    }


    @GetMapping(value = "/articles")
    public Object articles(@RequestParam Long channelId) {
        Page<Article> page = new PageFactory<Article>().defaultPage();
        page.addFilter(SearchFilter.build("idChannel", channelId));
        page.setSort(Sort.by(Sort.Direction.DESC, "createTime"));
        page = articleService.queryPage(page);
        return Rets.success(page);
    }

    @GetMapping("/article/{id}")
    public Object get(@PathVariable("id") Long id) {
        Article article = articleService.get(id);
        return Rets.success(article);
    }

    @PostMapping("/attend")
    public Object attend(@RequestBody ActionAttend attend) {
        Long idUser = getIdUser();
        if (null != idUser) {
            attend.setUserId(idUser);
            attend.setCreateBy(idUser);
            attend.setCreateTime(new Date());
            actionAttendService.insert(attend);
            return Rets.success("报名成功");
        } else {
            return Rets.failure("报名请登录");
        }
    }

    @GetMapping("/attended/{actionId}")
    public Object attended(@PathVariable("actionId") Long actionId) {
        List<SearchFilter> searchFilters = Lists.newArrayList();
        searchFilters.add(SearchFilter.build("actionId", SearchFilter.Operator.EQ, actionId));
        searchFilters.add(SearchFilter.build("userId", SearchFilter.Operator.EQ, getIdUser()));
        List<ActionAttend> actionAttends = actionAttendService.queryAll(searchFilters);
        return Rets.success(CollectionUtils.isEmpty(actionAttends) ? false : true);
    }
    @GetMapping("/attend/users/{actionId}")
    public Object attendUsers(@PathVariable("actionId") Long actionId) {
        List<SearchFilter> searchFilters = Lists.newArrayList();
        searchFilters.add(SearchFilter.build("actionId", SearchFilter.Operator.EQ, actionId));
        List<ActionAttend> actionAttends = actionAttendService.queryAll(searchFilters);
        return Rets.success(actionAttends);
    }
}
